<?php

class UploadFileCls {
	/**声明**/
	var $upfile_type, $upfile_size, $upfile_name, $upfile;
	var $d_alt, $extention_list, $tmp, $arri;
	var $datetime, $date;
	var $filestr, $size, $ext, $check;
	var $flash_directory, $extention, $file_path, $base_directory;
	var $url; //文件上传成功后跳转路径;
	var $issuccess;
	var $return='';


	function __construct() {
		/**构造函数**/
		$this->set_url ( "" ); //初始化上传成功后跳转路径;
		$this->set_extention (); //初始化扩展名列表;
		$this->set_size ( 2048 ); //初始化上传文件KB限制;
		$this->set_date (); //设置目录名称;
		$this->set_datetime (); //设置文件名称前缀;
		$this->set_base_directory ( "." ); //初始化文件上传根目录名，可修改！;
	}
	
	/**文件类型**/
	function set_file_type($upfile_type) {
		$this->upfile_type = $upfile_type; //取得文件类型;
	}
	
	/**获得文件名**/
	function set_file_name($upfile_name) {
		$this->upfile_name = $upfile_name; //取得文件名称;
	}
	
	/**获得文件**/
	function set_upfile($upfile) {
		$this->upfile = $upfile; //取得文件在服务端储存的临时文件名;
	}
	
	/**获得文件大小**/
	function set_file_size($upfile_size) {
		$this->upfile_size = $upfile_size; //取得文件尺寸;
	}
	
	/**设置文件上传成功后跳转路径**/
	function set_url($url) {
		$this->url = $url; //设置成功上传文件后的跳转路径;
	}
	
	/**获得文件扩展名**/
	function get_extention() {
		$this->extention = preg_replace ( '/.*\.(.*[^\.].*)*/iU', '\\1', $this->upfile_name ); //取得文件扩展名;
	}
	
	/**设置文件名称**/
	function set_datetime() {
		$this->datetime = date ( "YmdHis" ); //按时间生成文件名;
	}
	
	/**设置目录名称**/
	function set_date() {
		$this->date = date ( "Y-m-d" ); //按日期生成目录名称;
	}
	
	/**初始化允许上传文件类型**/
	function set_extention() {
		$this->extention_list = "doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png"; //默认允许上传的扩展名称;
	}
	
	/**设置最大上传KB限制**/
	function set_size($size) {
		$this->size = $size; //设置最大允许上传的文件大小;
	}
	
	/**初始化文件存储根目录**/
	function set_base_directory($directory) {
		//echo $directory;
		//$directory = dirname($directory);
		$directory = preg_replace('/[\\/]/',DIRECTORY_SEPARATOR,$directory);
		$this->base_directory = $directory; //生成文件存储根目录;
		
	}
	
	/**初始化文件存储子目录**/
	function set_flash_directory() {
		$this->flash_directory = $this->base_directory .DIRECTORY_SEPATRATOR. $this->date; //生成文件存储子目录;
	}
	
	/**错误处理**/
	function showerror($errstr = "未知错误！") {
		$this->return = json_encode(array('error'=>$errstr,'issuccess'=>0));
		exit;
		echo "alert('$errstr');location='javascript:history.go(-1);';";
		exit ();
	}
	
	/**跳转**/
	function go_to($str, $url) {
		//echo "alert('$str');location='$url';";
		//exit ();
	}
	
	/**如果根目录没有创建则创建文件存储目录**/
	function mk_base_dir() {
		$base_arr = explode( DIRECTORY_SEPARATOR ,$this->base_directory);
		$base_arr = array_diff($base_arr,array('',null));
		$temp_dir = '';
		//print_r($base_arr);
		if (is_array($base_arr) )
		foreach ($base_arr as $key=>$value)
		{
			if ($temp_dir=='')
			{
				$temp_dir =  $value .(DIRECTORY_SEPARATOR);
			}else{
				$temp_dir .= $value . DIRECTORY_SEPARATOR;
			}
			
			//echo $temp_dir.'||';
			//var_dump(file_exists ( $temp_dir ));
			if (!empty($temp_dir) && !file_exists ( $temp_dir )) { //检测根目录是否存在;
				mkdir ( $temp_dir, 0777 ); //不存在则创建;
			}
		}
		return;
		
		$num = substr_count($this->base_directory,DIRECTORY_SEPATRATOR);
		//echo $this->base_directory;echo '<br/>';
		$tem_pos = 0;
		for ($i=0;$i<$num;$i++)
		{
			
			$tem_pos = strpos ($this->base_directory,DIRECTORY_SEPATRATOR,$tem_pos+1);//echo "||{$tem_pos}<br/>";
			$temp = substr( $this->base_directory,0,strpos ($this->base_directory,DIRECTORY_SEPATRATOR,$tem_pos) );
			//echo '<br/>';
			if (! file_exists ( $temp ) ){ //检测根目录是否存在;
				mkdir ( $temp, 0777 ); //不存在则创建;
			}
		}
		if (! file_exists ( $this->base_directory )) { //检测根目录是否存在;
			mkdir ( $this->base_directory, 0777 ); //不存在则创建;
		}
		//*/
	}
	
	/**如果子目录没有创建则创建文件存储目录**/
	function mk_dir($name='') {
		if ($name=='')
		if (! file_exists ( $this->flash_directory )) { //检测子目录是否存在;
			@mkdir ( $this->flash_directory, 0777 ); //不存在则创建;
		}
	}
	
	/**以数组的形式获得分解后的允许上传的文件类型**/
	function get_compare_extention() {
		$this->ext = explode ( "|", $this->extention_list ); //以"|"来分解默认扩展名;
	}
	
	/**检测扩展名是否违规**/
	function check_extention() {
		for($i = 0; each ( $this->ext ); $i ++){ //遍历数组;
			if ($this->ext [$i] == strtolower ( $this->extention )){ //比较文件扩展名是否与默认允许的扩展名相符;

				$this->check = true; //相符则标记;
				break;
			}
		}
		if (! $this->check) {
			$this->showerror ( "正确的扩展名必须为" . $this->extention_list . "其中的一种！" );
		}
		//不符则警告
	}
	
	/**检测文件大小是否超标**/
	function check_size() {
		if ($this->upfile_size > round ( $this->size * 1024 )) //文件的大小是否超过了默认的尺寸;
{
			$this->showerror ( "上传附件不得超过" . $this->size . "KB" ); //超过则警告;
		}
	}
	
	/**文件完整访问路径**/
	function set_file_path($path_name='') {
		if ($path_name=='')
		{
			$this->file_path = $this->flash_directory . DIRECTORY_SEPARATOR . $this->datetime ."-".$this->upfile_name."." . $this->extention; //生成文件完整访问路径;
		} else {
			$this->file_path = $this->base_directory.DIRECTORY_SEPARATOR.$path_name;
		}
	}
	
	/**上传文件**/
	function copy_file() {
		//echo $this->upfile.'***'.$this->file_path;
		if ( DIRECTORY_SEPARATOR == '\\')
		{
			$this->file_path = mb_convert_encoding($this->file_path,'gbk','utf-8');
		}
		if (file_exists( $this->file_path ))
		{
			$temp = strrpos($this->file_path,DIRECTORY_SEPARATOR)+1;
			$date_mow = date("Y-m-d");
			$this->file_path = substr($this->file_path,0,$temp)."$date_mow"."_".rand(0,9)."_".substr($this->file_path,$temp);
		}
		if (copy ( $this->upfile, $this->file_path )) { //上传文件;
			//print $this->go_to ( "文件已经成功上传！", $this->url ); //上传成功;
			$this->issuccess = 1;
		} else {
			//print $this->showerror ( "意外错误，请重试！" ); //上传失败;
			$this->issuccess = 0;
		}
	}
	
	/**完成保存**/
	function save($name='') {
		$this->set_flash_directory (); //初始化文件上传子目录名;
		$this->get_extention (); //获得文件扩展名;
		$this->get_compare_extention (); //以"|"来分解默认扩展名;
		$this->check_extention (); //检测文件扩展名是否违规;
		$this->check_size (); //检测文件大小是否超限;   
		$this->mk_base_dir (); //如果根目录不存在则创建；
		$this->mk_dir ($name); //如果子目录不存在则创建;
		$this->set_file_path ($name); //生成文件完整访问路径;
		$this->copy_file (); //上传文件;
	}
	//
	function file_save($base_dir,$name='',$file_array='')
	{
		if ( $file_array == '')
		{
			reset($_FILES);
			$file_array = current($_FILES);
		}
		
		$this->set_base_directory($base_dir);
		$this->set_file_type($file_array['type']);
		$this->set_file_size($file_array['size']);
		//$file_array['name'] = mb_convert_encoding($file_array['name'],'utf-8','gb2312');
		$this->set_file_name($file_array['name']);
		$this->set_upfile($file_array['tmp_name']);
		if ($name=='')
		{
			$name = $this->upfile_name;
		}
		$this->save($name);
		if ( DIRECTORY_SEPARATOR == '\\')
		{
			$this->file_path = mb_convert_encoding($this->file_path,'utf-8','gbk');
			$this->file_path = substr($this->file_path,1);
			$this->file_path = strtr($this->file_path, DIRECTORY_SEPARATOR ,'/');
		}else {
			$this->file_path = substr($this->file_path,1);
		}
		
		$this->return = json_encode(array_merge($file_array,
												array('issuccess'=>$this->issuccess),
												array('src'=>($this->file_path))
												));
		return $this->return;
	}
	
}
//测试
//header("Content-type:text/html;charset=utf-8");
//$uploadFile = new UploadFileCls();
//$uploadFile->extention_list = "doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png"; //默认允许上传的扩展名称;
//$uploadFile->file_save('./a/e','fuck.jpg');
//echo $uploadFile->reutrn;
?>
